
import requests
import xlwt   # 写Excel文件，选用该模块
import xlrd   # 读取Excel文件中数据，选用该模块
from xlutils.copy import copy


def write_row_data(obj):  # obj指的是新添加的小列表，sname指的是sheet名字
    wb = xlrd.open_workbook(r'D:\resquestsdemo\zuoye.xls')
    sheet = wb.sheet_by_name('zuoye')
    nrows = sheet.nrows
    new_wb = copy(wb)  # 将原有的Excel，拷贝一个新的副本
    new_sheet = new_wb.get_sheet(0)  # 重新在新的Excel中获取
    for i in range(len(obj)):
        new_sheet.write(nrows, i, obj[i])
    new_wb.save(r'D:\resquestsdemo\zuoye.xls')
    return '存入成功！'

'''


url_string =r'https://douban.uieee.com/v2/movie/top250'
response = requests.get(url_string)
# print(response.text)
obj = response.json()
for move_list in obj['subjects']:
    grade = move_list['rating']['average']
    # print('评分',grade)
    mold = move_list['genres']
    # print('类型',mold)
    film_title = move_list['title']
    # print('电影名',film_title)
    actor = []
    for k in move_list['casts']:
        actor.append(k['name'])
    # print('演员',actor)
    duration = move_list['durations'][0]
    # print('时长',duration)
    direc = move_list['directors'][0]['name']
    # print('导演',direc)
    time_TV = move_list['year']
    # print('上映时间',time_TV)
    ID = move_list['id']
    # print(ID)
    sss = [film_title]+[mold]+[duration]+[time_TV]+[direc]+[actor]+[grade]+[ID]
    print(sss)
    write_row_data(sss)



# grade = move_list['rating']['average']
# print('评分',grade)
# mold = move_list['genres']
# print('类型',mold)
# film_title = move_list['title']
# print(film_title)
# actor = []
# for k in move_list['casts']:
#     actor.append(k['name'])
# print('演员',actor)
# duration = move_list['durations'][0]
# print('时长',duration)
# direc = move_list['directors'][0]['name']
# print('导演',direc)
# time_TV = move_list['year']
# print('上映时间',time_TV)
# ID = move_list['id']
# print(ID)
'''




